import json

import pandas as pd

from datareport.impl.paragraph.base.BaseParagraph import BaseParagraph
from datareport.api.annotation.Style import style


@style()
class ErJi_GuanMoQingKuang_29(BaseParagraph):
    """
    二级党组织
    """

    def __init__(self):
        super().__init__()
        self.text = '''2、从二级党组织来看：'''
        self.sort = 18
        self.section = 8

    def plot(self, df, plt):
        pass

    def getData(self, result):
        pass

    def start(self, conn, year, plt):
        df=pd.read_sql('''
        select partyOrgName '二级党组织',observer,concat(CONVERT(month(startTime),char),'月') '月份',1 '场次' from t_meetingInfo 
        where completeStatus='通过' and year(createTime)={} and observer is not null
        '''.format(year),con=conn)

        df['观摩人次']=df['observer'].apply(count)
        result=df.groupby(['二级党组织','月份']).agg({'场次':'sum','观摩人次':'sum'}).reset_index()
        self.tables.append(result)

def count(ob):
    ob_arr=json.loads(ob)
    return len(ob_arr)

if __name__ == '__main__':
    import yaml;
    from yaml import SafeLoader
    from datareport.api.DataSource import DataSource
    with open('D:\work\sanhuiyike\datareport\config.yaml', encoding='utf-8') as f:
        data = yaml.load(f, Loader=SafeLoader)
    con = DataSource(data['datasource']).conn
    ErJi_GuanMoQingKuang_29().start(con, 2023, '')





